package com.maoshi.shop.user.api.controller;

import com.maoshi.shop.bean.app.dto.ProductDto;
import com.maoshi.shop.bean.enums.ProdType;
import com.maoshi.shop.bean.model.Product;
import com.maoshi.shop.bean.model.Sku;
import com.maoshi.shop.common.exception.MaoshiShopBindException;
import com.maoshi.shop.service.ProductService;
import com.maoshi.shop.service.SkuService;
import com.maoshi.shop.user.comment.service.UserLevelService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import ma.glasnost.orika.MapperFacade;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;




@RestController
@RequestMapping("/score/prod")
@Api(tags = "积分商品接口")
public class ScoreProdController {

    @Autowired
    private ProductService prodService;

    @Autowired
    private MapperFacade mapperFacade;

    @Autowired
    private SkuService skuService;

    @Autowired
    private UserLevelService userLevelService;





    @GetMapping("/prodInfo")
    @ApiOperation(value = "积分商品详情信息", notes = "根据商品ID（prodId）获取商品信息")
    @ApiImplicitParam(name = "prodId", value = "商品ID", required = true, dataType = "Long")
    public ResponseEntity<ProductDto> prodInfo(Long prodId) {

        Product product = prodService.getProductByProdId(prodId);
        if (product == null || product.getStatus() != 1) {
            throw new MaoshiShopBindException("商品已下线");
        }
        if (!Objects.equals(product.getProdType(), ProdType.PROD_TYPE_SCORE.value()) || product.getScorePrice() <= 0) {
            throw new MaoshiShopBindException("不是积分商品或者积分价格异常！");
        }

        List<Sku> skuList = skuService.listByProdId(prodId);
        // 启用的sku列表
        List<Sku> useSkuList = skuList.stream().filter(sku -> sku.getStatus() == 1).collect(Collectors.toList());
        product.setSkuList(useSkuList);
        ProductDto productDto = mapperFacade.map(product, ProductDto.class);
        return ResponseEntity.ok(productDto);
    }




}
